package com.augurit.common.system.web.org;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtEditorGridSupport;
import com.augurit.common.base.extmodel.ExtEditorGridResult;
import com.augurit.common.system.service.org.IAtServerConvertService;
import com.augurit.common.system.web.org.form.AtServerConvertForm;
import com.augurit.common.util.ExtUtils;

/**
 * Web服务器访问地址转换Action类
 */
@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "at-server-convert.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/system/org/at-server-convert.jsp")
})
public class AtServerConvertAction extends ExtEditorGridSupport<AtServerConvertForm> {

	// Service 引用
	@Autowired
	private IAtServerConvertService atServerConvertService;
	
	// 基本属性
	private Page<AtServerConvertForm> page = new Page<AtServerConvertForm>(15);
	

	/**
	 * 系统首页的默认入口方法
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 分页获取Web服务器访问地址转换对象列表
	 */
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		atServerConvertService.getAll(page);
		this.extRenderGridJson(page);
		return null;
	}

	/**
	 * 新增Web服务器访问地址转换对象
	 */
	public String create() throws Exception {
		List<AtServerConvertForm> list = persistChangeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridCreateMessage(list), list, false);
		return null;
	}
	
	/**
	 * 更新Web服务器访问地址转换对象
	 */
	public String update() throws Exception {
		List<AtServerConvertForm> list = persistChangeForms();
		this.extRenderEditorGridResult(ExtEditorGridResult.getEditorGridUpdateMessage(list), list, true);
		return null;
	}

	/**
	 * 删除Web服务器访问地址转换对象
	 */
	public String delete() throws Exception {
		Long[] ids = result.getDeletedIds();
		atServerConvertService.delete(ids);
		this.extRenderEditorGridResult(true, ExtEditorGridResult.getEditorGridDeleteMessage(ids));
		return null;
	}

	/**
	 * 保存新增或修改的Web服务器访问地址转换对象
	 */
	private List<AtServerConvertForm> persistChangeForms() throws Exception{
		List<AtServerConvertForm> list = result.getChangedForms(AtServerConvertForm.class);
		atServerConvertService.save(list);
		return list;
	}
}
